from flask_restful import Resource, reqparse,request
from ....Model.Models import Report as ReportModel
from ....utils import timestamp,response
from flask_restful_swagger import swagger


# report_poot_parser = reqparse.RequestParser()
# report_poot_parser.add_argument('content', type=str)
# report_poot_parser.add_argument('informaiton_id', type=int)
# report_poot_parser.add_argument('report_user_id', type=int)
# report_poot_parser.add_argument('report_user_id', type=int)


success = 1
notfond = -1
error = 0

'''
举报表
content = 内容
informaiton_id = 资讯id
report_user_id = 举报者id
create_user_id = 资讯发布者id
'''

class Reportlist(Resource):
    def __init__(self, db, auth=None):
        self.db = db
    def get(self):#获取列表
        try:
            return response(success,None, [each.as_dict() for each in ReportModel.query.all()])
        except Exception as er:
            return response(error,er,None)
    def put(self):#插入数据
        try:
            args = report_poot_parser.parse_args()
            self.db.session.add(
                    ReportModel(
                        content=args['content'],
                        informaiton_id=args['informaiton_id'],
                        report_user_id =args['report_user_id'],
                        create_user_id =args['create_user_id'],
                        update_at=timestamp(),
                        create_at=timestamp()
                    )
                )
            self.db.session.commit
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)

class Report(Resource):
    def __init__(self, db, auth=None):
        self.db = db
    def get(self,report_id):#通过id获取report
        try:
            return response(success,None,ReportModel.query.filter_by(id=report_id).first().as_dict())
        except Exception as er:
            return response(error,er,None)

    def delete(self,report_id):#通过id删除
        try:
            self.db.session.delete(
                ReportModel.query.filter_by(id=report_id).first()
            )
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)
    def put(self,report_id):#修改
        try:
            reuport_obj = self.db.session.query.filter_by(id=report_id).first()
            args = user_poot_parser.parse_args()
            if(args['content'] != None):
                reuport_obj.content = args['content']
            if(args['informaiton_id'] != None):
                reuport_obj.informaiton_id = args['informaiton_id']
            if(args['report_user_id'] != None):
                reuport_obj.report_user_id = args['report_user_id']
            if(args['create_user_id'] != None):
                reuport_obj.create_user_id = args['create_user_id']
            reuport_obj.update_at = timestamp()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)
